package org.zjt.mock.demo.mock.config;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import java.io.IOException;
import java.util.Objects;

/**
 * Description:
 *
 * @author juntao.zhang
 * Date: 2018-06-21 下午5:53
 * @see
 */
@Slf4j
public class EncodingFilter implements Filter{

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        String characterEncoding = servletRequest.getCharacterEncoding();
        if (!Objects.equals(characterEncoding,"UTF-8")){
            log.error("异常字符集:{}",characterEncoding);
        }else{
            servletRequest.setCharacterEncoding("UTF-8");
        }
        servletResponse.setCharacterEncoding("UTF-8");
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }
}
